<template>
  <el-dialog
    :title="!dataForm.houseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房产名称" prop="houseName">
      <el-input v-model="dataForm.houseName" placeholder="房产名称"></el-input>
    </el-form-item>
    <el-form-item label="房产标题" prop="houseTitle">
      <el-input v-model="dataForm.houseTitle" placeholder="房产标题"></el-input>
    </el-form-item>
    <el-form-item label="房产面积" prop="houseArea">
      <el-input v-model="dataForm.houseArea" placeholder="房产面积"></el-input>
    </el-form-item>
    <el-form-item label="房产大小" prop="houseSize">
      <el-input v-model="dataForm.houseSize" placeholder="房产大小"></el-input>
    </el-form-item>
    <el-form-item label="房产价格" prop="housePrice">
      <el-input v-model="dataForm.housePrice" placeholder="房产价格"></el-input>
    </el-form-item>
    <el-form-item label="0未出租 1出租中" prop="houseStatus">
      <el-input v-model="dataForm.houseStatus" placeholder="0未出租 1出租中"></el-input>
    </el-form-item>
    <el-form-item label="房产图片" prop="houseImg">
      <el-input v-model="dataForm.houseImg" placeholder="房产图片"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="housePayType">
      <el-input v-model="dataForm.housePayType" placeholder="支付方式"></el-input>
    </el-form-item>
    <el-form-item label="房产特点" prop="houseTrait">
      <el-input v-model="dataForm.houseTrait" placeholder="房产特点"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          houseId: 0,
          houseName: '',
          houseTitle: '',
          houseArea: '',
          houseSize: '',
          housePrice: '',
          houseStatus: '',
          houseImg: '',
          housePayType: '',
          houseTrait: ''
        },
        dataRule: {
          houseName: [
            { required: true, message: '房产名称不能为空', trigger: 'blur' }
          ],
          houseTitle: [
            { required: true, message: '房产标题不能为空', trigger: 'blur' }
          ],
          houseArea: [
            { required: true, message: '房产面积不能为空', trigger: 'blur' }
          ],
          houseSize: [
            { required: true, message: '房产大小不能为空', trigger: 'blur' }
          ],
          housePrice: [
            { required: true, message: '房产价格不能为空', trigger: 'blur' }
          ],
          houseStatus: [
            { required: true, message: '0未出租 1出租中不能为空', trigger: 'blur' }
          ],
          houseImg: [
            { required: true, message: '房产图片不能为空', trigger: 'blur' }
          ],
          housePayType: [
            { required: true, message: '支付方式不能为空', trigger: 'blur' }
          ],
          houseTrait: [
            { required: true, message: '房产特点不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.houseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.houseId) {
            this.$http({
              url: this.$http.adornUrl(`/rent/rent/info/${this.dataForm.houseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.houseName = data.rent.houseName
                this.dataForm.houseTitle = data.rent.houseTitle
                this.dataForm.houseArea = data.rent.houseArea
                this.dataForm.houseSize = data.rent.houseSize
                this.dataForm.housePrice = data.rent.housePrice
                this.dataForm.houseStatus = data.rent.houseStatus
                this.dataForm.houseImg = data.rent.houseImg
                this.dataForm.housePayType = data.rent.housePayType
                this.dataForm.houseTrait = data.rent.houseTrait
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rent/rent/${!this.dataForm.houseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'houseId': this.dataForm.houseId || undefined,
                'houseName': this.dataForm.houseName,
                'houseTitle': this.dataForm.houseTitle,
                'houseArea': this.dataForm.houseArea,
                'houseSize': this.dataForm.houseSize,
                'housePrice': this.dataForm.housePrice,
                'houseStatus': this.dataForm.houseStatus,
                'houseImg': this.dataForm.houseImg,
                'housePayType': this.dataForm.housePayType,
                'houseTrait': this.dataForm.houseTrait
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
